#include "kdcmediaplayerplugin.h"

#include <KPluginFactory>
#include <KPluginLoader>

class KDCMediaPlayerPlugin;

#include <KLocale>
#include <kdclib.h>
#include <QVBoxLayout>

#include "kdcmediaplayerconfigpage.h"
#include <kdcmplayerwindow.h>

K_PLUGIN_FACTORY( KDCMediaPlayerPluginFactory, registerPlugin< KDCMediaPlayerPlugin >(); )
K_EXPORT_PLUGIN( KDCMediaPlayerPluginFactory( "KDCMediaPlayerPlugin" ) )


KDCMediaPlayerPlugin::KDCMediaPlayerPlugin( QObject *parent, const QVariantList &args ) : KDCPluginInterface( parent ), pInfo( KPluginInfo("kdcmediaplayerplugin.desktop", "services") ), pInterfaces( NULL ), pMediaPlayerFactoryImpl( NULL ), pMainWidget( NULL )
{

}

KDCMediaPlayerPlugin::~KDCMediaPlayerPlugin()
{
	unload();
}

QString KDCMediaPlayerPlugin::interfaceName() const
{
	return "KDCMediaPlayerPlugin";
}

const KPluginInfo& KDCMediaPlayerPlugin::info() const
{
	return pInfo;
}

QWidget* KDCMediaPlayerPlugin::widget() const
{
	return pMainWidget;
}

KDCConfigPageInterface* KDCMediaPlayerPlugin::configPage() const
{
	return new KDCMediaPlayerConfigPage;
}

KDC::Position KDCMediaPlayerPlugin::tabPosition() const
{
	return KDC::Center;
}

bool KDCMediaPlayerPlugin::load()
{
	pMediaPlayerFactoryImpl = new MediaPlayerFactoryImpl( this );
	pMainWidget = new QWidget;
	QVBoxLayout *vLayout = new QVBoxLayout( pMainWidget );
	vLayout->addWidget( pMediaPlayerFactoryImpl->mainInstance()->window(), 10 );
	vLayout->addWidget( pMediaPlayerFactoryImpl->mainInstance()->controlPanel() );
	return true;
}

bool KDCMediaPlayerPlugin::unload()
{
	pInterfaces->removeOne( pMediaPlayerFactoryImpl );
	deletePtr( pMediaPlayerFactoryImpl );
	return true;
}

bool KDCMediaPlayerPlugin::setInterfaces( QList<KDCBaseInterface*>& aInterfaces )
{
	pInterfaces = &aInterfaces;
	aInterfaces.append( pMediaPlayerFactoryImpl );
	return true;
}

bool KDCMediaPlayerPlugin::init()
{
	foreach( KDCBaseInterface *interface, *pInterfaces )
		if ( interface->interfaceName() == "KDCDiscStructurePlugin" )
		{
			connect( static_cast<KDCPluginInterface*>( interface ), SIGNAL( objectChanged( KDCObject * ) ), this, SLOT( setObject( KDCObject * ) ) );
			return true;
		}
	return true;
}

KDCObject* KDCMediaPlayerPlugin::currentObject() const
{
	return NULL;
}

void KDCMediaPlayerPlugin::refresh()
{

}

void KDCMediaPlayerPlugin::setObject( KDCObject *aObject )
{
	if ( aObject && aObject->object() == KDC::Movie )
		pMediaPlayerFactoryImpl->mainInstance()->setMovie( static_cast<KDCMovie*>( aObject ) );
	else
		pMediaPlayerFactoryImpl->mainInstance()->setMovie( NULL );
}


#include "kdcmediaplayerplugin.moc"
